package character;
import java.util.ArrayList;
import java.util.List;

public class DFA {

	List<State> States;
	
	/**
	 * Creates a new DFA with a set list of States
	 * @param DFAStates
	 */
	public DFA(List<State> DFAStates){
		States = DFAStates;
	}
	

	/**
	 * Minimizes the current DFA
	 */
	public void minimize(){
		//Minimize the current DFA
	}
	
	/**
	 * Traverses the DFA based on the given string.  String is read in character-by-character
	 * and a boolean is returned based on a successful read
	 * @return
	 */
	public boolean traverse(String testCode){
		
		//Our DFA is set so we only have to read in character by character.
		State StartState = null;
		int DFAsize = this.States.size();
		for(int k=0; k<DFAsize; k++){
			State state = this.States.get(k);
			if(state.isStartState()){
				StartState = state;
			}
		}
		
		State traveler = StartState;
		for(int k=0; k<testCode.length(); k++){
			String testChar = Character.toString(testCode.charAt(k));
			List<Input> stateInputs = traveler.getInputs();
			Boolean charBool = false;
			for(int c=0; c<stateInputs.size(); c++){
				if(testChar.equals(stateInputs.get(c).getTitle())){
					traveler = stateInputs.get(c).getNextState();
					charBool = true;
				}
			}
			if(charBool==false){
				return false;
			}
		}
		
		if(traveler.isFinishState()){
			return true;
		}
		return false;
	}
	
	/**
	 * Returns the list of states
	 * @return
	 */
	public List<State> getStates() {
		return States;
	}

	/**
	 * Sets the list of states
	 * @param states
	 */
	public void setStates(List<State> states) {
		States = states;
	}
}
